import React, {Component, PropTypes} from 'react';
import {hashHistory} from 'react-router';

import {Link} from 'react-router';
import classnames from 'classnames';
import {_Card,} from 'ANT_DE';
const Card = _Card;

import {Tooltip} from 'antd';
import Config from "../../../../util/Config"

export default class  extends Component {
    constructor(props) {
        super(props);
        this.state = {}

        this.downloadFile = this.downloadFile.bind(this);
        this.orderDetail = this.orderDetail.bind(this);
    }

    componentWillMount(){}

    componentDidMount(){}

    downloadFile(e){
        e.stopPropagation();

        const data = this.props.data,
            isSend = this.props.isSend;

        //下载资源
        const invo = isSend ? data.invoiceOutJson == null ?  {} : data.invoiceOutJson : data.invoiceInJson == null ? {} : data.invoiceInJson,
            d = invo.invoiceFileJson,a = document.createElement("a");
            a.setAttribute("download","");

        Array.isArray(d) && d.forEach((i,k)=>{

            const h = `${Config.qiuniu+i.response.key}?attname=${i.name}`;

            a.href = h;
            a.click();
        });
    }

    orderDetail(id){
        hashHistory.push(`/orderdetailcloth/${id}`)
    }

    render() {
        const {
            className,
            data,
            isSend
        }  = this.props;

        const name = data ? isSend ? data.partaName : data.partbName : "";

        let invo;

        if(data){
            invo = isSend ? data.invoiceOutJson == null ?  {} : data.invoiceOutJson : data.invoiceInJson == null ? {} : data.invoiceInJson;
        }

        return <Tooltip title={name || "" } overlayClassName="order_Tree--Card___alarm">
            <div className={classnames({"order_Tree--Card":true,"order_Tree_card-send":isSend,[className]:true})}>
                <div className="order_Tree--Card__title">
                    {name ? name.slice(0,6) : name}
                </div>
                <div className="order_Tree--Card__content">
                    <div className="order_Tree--Card___detail">
                        <div className="order_Tree--Card___item">
                          订单编号 :  <a  onClick={()=>{this.orderDetail(data.id)}} data-type="code" title={data.id}> {data.id} </a>
                        </div>
                        <div className="order_Tree--Card___item">
                            <span> 合同编号 :</span> {data.contract || "-"}
                        </div>
                       <div className="order_Tree--Card___item">
                           <span> 发票金额 :</span> { invo.invoiceMoney || "-"}
                       </div>
                       <div className="order_Tree--Card___item">
                           <span> 发票张数 :</span> { invo.invoiceNum || "-"}
                       </div>
                        <div className="order_Tree--Card___item">
                                <span> 发票附件 :</span>
                            {
                                (invo.invoiceFileJson && invo.invoiceFileJson.length > 0)  ?
                                <a onClick={this.downloadFile}> 下载 </a> : "-"
                            }
                        </div>
                    </div>
                </div>
            </div>
        </Tooltip>
    }
}